import 'package:flutter/material.dart';
import 'package:flycht/pages/android_back_desktop.dart';
import 'package:flycht/pages/home/home.dart';
import 'package:flycht/pages/login/login.dart';
import 'package:shared_preferences/shared_preferences.dart';

class WelcomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    _IsLogin(context);
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Sample App',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: new Scaffold(
          body: new Center(
            child: Text('欢迎'),
          ),
        ));
  }

  _IsLogin(context) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    Object username = prefs.get('username');
    Object token = prefs.get('token');
    print('$username  $token');
    if (username != null && token != null) {
      //跳转并关闭当前页面
      Navigator.pushAndRemoveUntil(
        context,
        new MaterialPageRoute(
            builder: (context) => WillPopScope(
                  onWillPop: () async {
                    backDeskTop(); //设置为返回不退出app
                    return false;
                  }, //页面将要消失时，调用原生的返回桌面方法
                  child: MaterialApp(
                    home: DrawerHome(),
                  ),
                )),
        (route) => route == null,
      );
    } else {
      //跳转并关闭当前页面
      Navigator.pushAndRemoveUntil(
        context,
        new MaterialPageRoute(
            builder: (context) => WillPopScope(
                  onWillPop: () async {
                    backDeskTop(); //设置为返回不退出app
                    return false;
                  }, //页面将要消失时，调用原生的返回桌面方法
                  child: MaterialApp(
                    home: LoginPage(),
                  ),
                )),
        (route) => route == null,
      );
    }
  }
}
